<?php
/**
 * SearchController is an instance of Controller (application.components). 
 *
 * Controller is an instance of CController (system.web).
 * CController manages a set of actions which deal with the corresponding user requests. 
 * Through the actions, CController coordinates the data flow between models and views. 
 *
 * Eduow :  International system for education (http://eduow.com)
 * Copyright 2010, Nikolay Stoitsev 
 *
 * Licensed under GNU General Public License version 2
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2010
 * @link          http://eduow.com Eduow Project
 * @package       eduow
 * @subpackage    application.controllers
 * @inheritance   class SearchController » Controller » CController » CBaseController » CComponent
 * @implements    
 * @since         Eduow v 0.0.1
 * @license       http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License version 2
 */


class SearchController extends Controller
{

	/**
	 * @var array fields for the search query
	 */
	private $fields = array();

	/**
	 * @var string the default layout for the views. 
	 */
	public $layout='main';


	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			'*'=>array('*'),
		);
	}

	/**
	 * Default action.
	 * Render seach form and provide results from seaching
	 */
	public function actionIndex()
	{

		$model = new SearchForm;

		if(isset($_POST['SearchForm']))
		{

		$model->attributes=$_POST['SearchForm'];
			if($model->validate()){

				switch ($model->type) {
				    case "post":
					$srch = 'Post';	
					$folder="post";
					$this->fields = array(0 => "title", 1 => "content");
					break;
				    case "user":
					$srch = 'User';	
					$folder="user";
					$this->fields = array(0 => "username", 1 => "email");
					break;
				}	

				if($folder){
					$dataProvider=new CActiveDataProvider($srch,array(
					'criteria'=>array(
						'condition'=>$this->squery($model->word)
						),
			    		'pagination'=>array(
			       			'pageSize'=>40,
			    			),
					));
				}
			}	
		}
		
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
			'model'=>$model,
			'folder'=>$folder
		));
	}

	  /*
	   * Create query for searching for articles
	   * Looks up table specified in @arrayFields for
	   * words specified in @keystring
	   * @return SQL query to be executer
	   */
	    protected function squery($keystring){
		//escaping input data
		$keystring = addslashes($keystring);

		$arraySearch = explode(" ", $keystring);
		//$arraySearchC = my_mb_ucfirst(explode(" ", $search));
		// table fields to search
		$arrayFields = $this->fields;
		$countSearch = count($arraySearch);
		$a = 0;
		$b = 0;
		$query = "";
		$countFields = count($arrayFields);
		while ($a < $countFields)
		{
		    while ($b < $countSearch)
		    {
		        $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
		        $cl = $arraySearch[$b];
		        $cl = $this->my_mb_ucfirst($cl);
		        $query = $query." OR $arrayFields[$a] LIKE '%$cl%'";
		        $b++;
		        if ($b < $countSearch)
		        {
		            $query = $query." OR ";
		        }
		    }
		    $b = 0;
		    $a++;
		    if ($a < $countFields)
		    {
		        $query = $query." OR ";
		    }
		}
		$query = $query."";
		return $query;
	    }

	    protected function my_mb_ucfirst($str, $e='utf-8') {
		$fc = mb_strtoupper(mb_substr($str, 0, 1, $e), $e);
		return $fc.mb_substr($str, 1, mb_strlen($str, $e), $e);
	    }
}
